/**
 * 鱼主体
 */
class Fish extends eui.Component{
    //鱼主体
    private fish:eui.Image;
    //高等鱼闪光特效
    private fishQuiltyEffect:eui.Image;
    //鱼id
    private fishId:number;
    private fishData;
    public fishSpeed:number;
    //鱼方向 0左边 1右边
    public dir:number;
    
    //传入鱼id初始化鱼
    public constructor(id:number)
    {
        super();
        this.fishId = id;
        this.init();
        this.cacheAsBitmap = true;
    }

    public init()
    {
        this.fishData = FishTpl.instance().getDataById(this.fishId);
        if(this.fishData)
        {
            this.fishSpeed = FishTpl.instance().getSpeedByQuilty(this.fishData['quilty']) * Math.random() * 2;
            this.addFish();
            if(this.fishData['quilty'] >= 4)
            {
                this.addBlinkEffect();
            }
        }
        else
        {
            LogUtil.error("FishId don't exit in the fishTpl=="+this.fishId);
        }
    }

    //改变鱼的方向
    public changeFishDir()
    {
        if(this.fish)
        {
            this.dir = this.dir == DirType.left ? DirType.right : DirType.left;
            this.fish.scaleX *= -1;
            if(this.dir == DirType.right)
            {
                this.fish.x = this.width;
            }
            else
            {
                this.fish.x = 0;
            }
            this.fishSpeed = FishTpl.instance().getSpeedByQuilty(this.fishData['quilty']) * Math.random() * 2;
        }
    }

    //添加鱼主体
    protected addFish()
    {
        this.dir = Math.floor(Math.random()*2);
        this.fish = new eui.Image();
        this.fish.texture = RES.getRes(this.fishData['icon']);
        this.width = this.fish.width;
        this.height = this.fish.height;

        if(this.dir == DirType.right)
        {
            this.fish.x = this.width;
            this.fish.scaleX = -1;
        }
        this.addChild(this.fish);
    }

    //添加闪光特效
    public addBlinkEffect()
    {
        this.fishQuiltyEffect = new eui.Image();
        let texture = RES.getRes('qulity_light_png');
        
        this.fishQuiltyEffect.texture = texture;
        this.fishQuiltyEffect.x = this.width>> 1;
        this.fishQuiltyEffect.y = this.height>> 1;
        this.addChildAt(this.fishQuiltyEffect, 0);
        this.fishQuiltyEffect.scaleX = this.fishQuiltyEffect.scaleY = 0.8;

        this.fishQuiltyEffect.anchorOffsetX = this.fishQuiltyEffect.width >> 1;
        this.fishQuiltyEffect.anchorOffsetY = this.fishQuiltyEffect.height >> 1;
        EffectUtil.instance().rotationEffect(this.fishQuiltyEffect, 2000);
    }

    public getSpeed()
    {
        return this.fishSpeed;
    }

    public getFishData()
    {
        return this.fishData;
    }

    //碰撞之后做的事情
    public afterCollision()
    {
        this.clear();
        this.fish.scaleX = 1;
        this.fish.x = 0;
        if(this.fishQuiltyEffect)
        {
            EffectUtil.instance().StopEffect(this.fishQuiltyEffect);
            this.removeChild(this.fishQuiltyEffect);
            this.fishQuiltyEffect = null;
        }
        
        //需要翻转的鱼加动画
        if(this.fishData.isNeedRotation == 1)
        {
            this.anchorOffsetY = this.height >> 1;
            this.rotation = 90;
            egret.Tween.get(this, { loop: true }).to({ rotation: 120 }, 250).wait(100).to({ rotation: 60 }, 500).wait(100).to({ rotation: 120 }, 500);
        }
        else
        {
            this.anchorOffsetX = this.width >> 1;
            egret.Tween.get(this, { loop: true }).to({ rotation: -30 }, 250).wait(100).to({ rotation: 60 }, 500).wait(100).to({ rotation: -30 }, 500);
        }
    }

    public clear()
    {
        if(this.parent)
           this.parent.removeChild(this); 
    }

    public destory()
    {
        if(this.parent)
            this.parent.removeChild(this);
        egret.Tween.removeTweens(this);
        this.fishSpeed = null;
        this.fishData = null;
        this.fish = null;
        this.fishId = null;
        if(this.fishQuiltyEffect)
        {
            egret.Tween.removeTweens(this.fishQuiltyEffect);
            this.fishQuiltyEffect = null;
        }
        this.dir = null;
    }
}